package com.hardydou.data.controller;

import com.github.pagehelper.PageInfo;
import com.hardydou.common.controller.BaseController;
import com.hardydou.common.result.Result;
import com.hardydou.common.result.ResultAssert;
import com.hardydou.common.result.ResultHelper;
import com.hardydou.data.model.Film;
import com.hardydou.data.service.IFilmService;
import com.hardydou.framework.log.anno.LogMetaInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：hardy
 * @description：
 * @date ：Created in 2020/7/15
 * @Copyright ：Hardydou.com
 */
@Controller
@LogMetaInfo(module = "影视数据")
@RequestMapping(FilmController.REQ_PREFIX)
public class FilmController extends BaseController {
    final static String REQ_PREFIX = "/data/film";

    /***
     * 主模块名称
     * @return
     */
    @Override
    public String getModel() {
        return "data";
    }

    /***
     * 子模块名称
     * @see #page(String)
     * @return
     */
    @Override
    public String getSubModel() {
        return "film";
    }

    @Autowired
    IFilmService filmService;

    @GetMapping("")
    public String index() {
        return page("list");
    }

    @ApiIgnore
    @GetMapping("/add")
    public String add() {
        return page("add");
    }


    @ApiIgnore
    @PostMapping("/add")
    public String add(Film film, Model model) {
        filmService.add(film);
        model.addAttribute("code", 200);
        model.addAttribute("message", "添加成功");
        return page("add");
    }

    @ApiIgnore
    @GetMapping("/edit")
    public String edit(long resId, Model model) {
        Film film = filmService.findById(resId);
        model.addAttribute("film", film);
        return page("edit");
    }

    @ApiIgnore
    @PostMapping("/edit")
    public String edit(Film film, Model model) {
        model.addAttribute("code", 200);
        model.addAttribute("message", "修改成功");
        filmService.update(film);
        model.addAttribute("film", film);
        return page("edit");
    }


    @ApiIgnore
    @GetMapping("/info")
    public String info(long resId, Model model) {
        Film film = filmService.findById(resId);
        model.addAttribute("film", film);
        return page("info");
    }


    //-----------JSON API---------//

    @ApiOperation(value = "影视列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", required = true)
    })
    @GetMapping("/list.json")
    @ResponseBody
    public Result<?> listData(int pageNum, int pageSize, @RequestParam Map params) {
        List<Film> films = filmService.list(params);
        PageInfo<Film> pageInfo = new PageInfo<>(films);
        return ResultHelper.ok(pageInfo);
    }

    @ApiOperation(value = "首页资源")
    @GetMapping("/index.json")
    @ResponseBody
    public Result<?> indexData(String filmTypes) {
        Map data = new HashMap();
        data.put("latest", filmService.findLatest(6));
        data.put("favorite", filmService.findByType(filmTypes, 6));
        data.put("hot", filmService.findHots(6));
        return ResultHelper.ok(data);
    }

    @ApiOperation(value = "影视搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", required = true)
    })
    @GetMapping("/search.json")
    @ResponseBody
    public Result<?> searchData(int pageNum, int pageSize, String keywords) {
        //TODO 全文索引
        Map params = new HashMap();
        params.put("name", keywords);
        params.put("actor", keywords);
        params.put("director", keywords);
        params.put("intro", keywords);
        List<Film> films = filmService.list(params);
        PageInfo<Film> pageInfo = new PageInfo<>(films);
        return ResultHelper.ok(pageInfo);
    }

    @ApiOperation(value = "上/下架资源")
    @ApiImplicitParam(name = "resId", value = "资源ID", required = true)
    @PutMapping("/status")
    @ResponseBody
    public Result status(Long resId) {
        ResultAssert.notNull(resId);
        Film film = filmService.findById(resId);
        ResultAssert.notNull(film);
        boolean del = !film.isDel();
        film = new Film();
        film.setResId(resId);
        film.setDel(del);
        filmService.update(film);
        Result<Map<String, Object>> result = ResultHelper.ok();
        result.getData().put("resId", resId);
        result.getData().put("del", del);
        return result;
    }


}
